<template>
    <div class="admin-page">
        <div class="page-header">
            <div style="display: flex;justify-content: space-between">
                <div style="display: flex; flex-wrap: wrap; align-items: center; gap: 15px; margin-bottom: 15px;">
<!--                    <div style="display: flex; align-items: center; ">
                        <span>审核状态:</span>
                        <el-select v-model="form.state" size="small" style="margin-left: 10px; flex: 1;width: 150px;">
                            <el-option label="全部" value=""></el-option>
                            <el-option label="未审核" value="0"></el-option>
                            <el-option label="已审核" value="1"></el-option>
                            <el-option label="未通过" value="2"></el-option>
                        </el-select>
                    </div>-->
                    <div style="display: flex; align-items: center; ">
                        <span>用户ID:</span>
                        <el-input v-model="form.search" size="small" style="margin-left: 10px; width: 150px;" placeholder="请输入用户ID"></el-input>
                    </div>
                    <div style="display: flex; align-items: center;" class="search-item">
                        <span class="search-label">充值类型:</span>
                        <el-select v-model="form.type" size="small" style="width: 130px;" placeholder="全部">
                            <el-option label="系统充值" value="2"></el-option>
                            <el-option label="系统扣减" value="3"></el-option>
                        </el-select>
                    </div>
                    <div style="display: flex; align-items: center;" class="search-item">
                        <span class="search-label">金额类型:</span>
                        <el-select v-model="form.m_type" size="small" style="width: 130px;" placeholder="全部">
                            <el-option label="返利" value="2"></el-option>
                            <el-option label="余额" value="1"></el-option>
                        </el-select>
                    </div>
                </div>
                <div style="display: flex; gap: 15px; align-items: center">
                    <el-button type="primary" size="small" @click="toSearch">搜索</el-button>
                    <el-button type="success" size="small" @click="reset">重置</el-button>
                </div>
            </div>

            <el-table :data="tableData" height="560" border style="width: 100%;overflow-y: auto"
                      :header-cell-style="{background:'#FAFAFA',color:'#666', fontSize:'13px'}" size="small">
                <el-table-column prop="uid" label="ID" width="80" align="center" />
                <el-table-column prop="username" label="用户名" width="120" align="center" />
                <el-table-column prop="nickname" label="昵称" width="120" align="center" />
                <el-table-column prop="money" label="变动金额" align="center"  />
                <el-table-column align="center" prop="thumb" label="变动凭证" width="80">
                    <template slot-scope="scope">
                        <img style="width: 50px;height: 50px"  @click="openCertificateDialog(scope.row.thumb)" :src="$appConfig.imgPath + scope.row.thumb"/>
                    </template>
                </el-table-column>
                <el-table-column prop="type" label="充值类型" align="center"  width="100" />
                <el-table-column prop="m_type" label="金额类型" align="center"  width="100"/>
                <el-table-column prop="create_time" label="改变时间"  align="center" />
                <el-table-column prop="remark" label="备注"  align="center" />
                <el-table-column prop="admin_name" label="操作人"  align="center" />
            </el-table>
            <el-dialog title="变动凭证" :visible.sync="qrCodeDialogVisible" width="300px" :close-on-click-modal="true">
                <div style="text-align: center;">
                    <img :src="currentQRCode" style="max-width: 100%; max-height: 800px;">
                </div>
            </el-dialog>
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-size="pageSize"
                layout="total, prev, pager, next"
                :total="total"
            />
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            form: {
                // state: '',
                search: '',
                m_type: '',
                type: '',
            },
            tableData: [],
            statusOptions: [
                { label: '全部', value: '' },
                { label: '已发放', value: 1 },
                { label: '未发放', value: 0 }
            ],
            currentPage: 1,
            pageSize: 25,
            total: 0,
            searchParams: {},
            typeOptions: [],
            mTypeOptions: [],
        qrCodeDialogVisible:false,
            currentQRCode: '',
        }
    },
    created() {
        this.getTableData()
        this.typeList()
    },
    methods: {
        openCertificateDialog(thumb) {
            this.currentQRCode = this.$appConfig.imgPath + thumb;
            // this.currentQRCode = `http://www.ts.com/${thumb}`;
            this.qrCodeDialogVisible = true;
        },

        // 修复：合并搜索参数和分页参数
        getTableData(params = {}) {
            const requestParams = {
                page: this.currentPage,
                is_sys : 1,
                // is_test : 1,
                ...params
            };
            this.$http.get('/admin/SurplusGrain/lists', { params: requestParams }).then(result => {
                if (result.code === 200) {
                    this.tableData = Array.isArray(result.data.data) ? result.data.data : [];
                    this.total = parseInt(result.data.total) || 0;
                } else {
                    this.$message.error(result.msg);
                    this.tableData = [];
                    this.total = 0;
                }
            }).catch(error => {
                console.error("获取订单列表失败:", error);
                this.$message.error("获取订单列表失败，请稍后重试");
                this.tableData = [];
                this.total = 0;
            });
        },
        handleSizeChange(val) {
            this.pageSize = val;
            this.currentPage = 1;
            this.getTableData(this.searchParams);
        },
        handleCurrentChange(val) {
            this.currentPage = val;
            this.getTableData(this.searchParams);
        },
        toSearch() {
            const formatDate = (date) => {
                if (!date) return '';
                const d = new Date(date);
                const year = d.getFullYear();
                const month = String(d.getMonth() + 1).padStart(2, '0');
                const day = String(d.getDate()).padStart(2, '0');
                const hours = String(d.getHours()).padStart(2, '0');
                const minutes = String(d.getMinutes()).padStart(2, '0');
                const seconds = String(d.getSeconds()).padStart(2, '0');
                const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
                const timestamp = new Date(formattedDate).getTime();
                return Math.floor(timestamp / 1000); // 转换为秒级时间戳
            };

            this.searchParams = {
                ...this.form,
                start_time: formatDate(this.form.start_time),
                end_time: formatDate(this.form.end_time)
            };
            // Remove empty string properties from searchParams before sending
            Object.keys(this.searchParams).forEach(key => {
                if (this.searchParams[key] === '') {
                    delete this.searchParams[key];
                }
            });
            this.currentPage = 1;
            this.getTableData(this.searchParams);
        },
        reset() {
            this.form = {
                search: "",
                type: "",
                m_type: "",
                start_time: "",
                end_time: ""
            };
            this.searchParams = {};
            this.currentPage = 1;
            this.getTableData(); // 修复：重置后获取无参数数据
        },
    }
}
</script>

<style scoped lang="less">
.admin-page {
    padding: 20px;

    .search-label {
        margin-right: 10px;
        color: #606266;
    }

    .el-table {
        margin-top: 20px;
    }

    .el-pagination {
        margin-top: 20px;
        text-align: right;
    }
}
</style>
